﻿using System;
using System.Web.UI;

using BLL;
using Model;
using Utility;

namespace EvaluationSystem.Answer
{
    public partial class Tlogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtPart1.Focus();

            /*
            string broNm = Request.Browser.Browser;
            int broVer = Request.Browser.MajorVersion;

            if (broNm != "IE" || broVer < 8)
            {
                MessageBox.ShowAndRedirect(this, "您好，本系统的使用环境为IE8浏览器，点击确定后将进入下载界面", 
                @"http://windows.microsoft.com/zh-CN/internet-explorer/downloads/ie-8");
            }*/
        }

        protected void BtnLogin_Click(object sender, ImageClickEventArgs e)
        {
            if (!txtValidate.Text.ToUpper().Equals(Session["CheckCode"].ToString()))
            {
                MessageBox.ShowAndRedirect(this, "您输入的验证码有误，请重新输入！", "Tlogin.aspx");
                return;
            }
            
            GuidBLL guidBll = new GuidBLL();
            QuestionBLL qBll = new QuestionBLL();
            ActivityBLL aBll = new ActivityBLL();

            string userGUID = txtPart1.Text.Trim().ToUpper();
            
            int status = guidBll.GetStatus(userGUID);
            int nActID = guidBll.GetActivityId(userGUID);
            Activity activity = aBll.GetModel(nActID);

            if (-1 == status)//无效序列号
            {
                MessageBox.ShowAndRedirect(this, "您好，序列号无效，请输入正确的8位序列号", "Tlogin.aspx");
            }
            else if (2 == status)//测试完成过的序列号，如果测评项目允许查看报告，则跳转至相关界面。
            {
                if (activity != null && 
                    activity.Activity_viewreport.ToString().Substring(0,1).Equals("1"))
                {
                    MessageBox.ShowAndRedirect(this, "您好，该序列号已经测试完成，将转入报告查看页面",
                        "../Report/GenPdfReport.aspx?sn=" + userGUID);
                }
                else
                {
                    MessageBox.ShowAndRedirect(this, "您好，该序列号已经测试完成", "Tlogin.aspx");
                }
            }
            else if (0 == status || 1 == status)//正常，未使用过的序列号，或者正在测试的。
            {              
                if (activity != null)
                {
                    //是否为禁用的活动状态
                    if (activity.Activity_status == 0)
                    {
                        MessageBox.ShowAndRedirect(this, "您好，您参加的测评项目未开启，请联系发放序列号的相关人员", "Tlogin.aspx");
                    }
                    else
                    {
                        Session["isTesting"] = "true";//开始考试

                        if (status == 1 && CacheManager.Get("guid_" + userGUID) != null)
                        {
                            MessageBox.ShowAndRedirect(this, "将继续上次答题", "QuestionRepeat.aspx?guid=" + userGUID
                                           + "&logopath=" + activity.Activity_logo);
                        }
                        else
                        {
                            Response.Redirect("Introduce.aspx?guid=" + userGUID
                                                + "&logopath=" + activity.Activity_logo
                                                + "&weltext=" + activity.Activity_welcometext.Replace("\n", "")
                                                + "&atext=" + activity.Activity_content.Replace("\n", "")
                                                + "&ctext=" + activity.Activity_companyintrotext.Replace("\n", ""));
                        }
                    }
                }
            }
            else
            {
                MessageBox.ShowAndRedirect(this, "您好，该序列号无效", "Tlogin.aspx");
            }
        }
    }
}